//var language = ["he-HE", "ru-RU", "en-US"];
var languageType;
var login_block;
var work_images = ['<img src="../images/work2.jpg"/>', '<img src="../images/work.jpg"/>', '<img src="../images/work3.jpg"/>', '<img src="../images/work4.jpg"/>', '<img src="../images/work5.jpg"/>'];
var snd = new Audio("../images/incorrect.mp3");
var menuSelected = 0;

$(function() {
    //main page ****************************************************************
    //choice language
//    if (userLang == language[1]) {
//        $.each($(".menu_language ul li"), function() {
//            if ($(this).text() == "RU") {
//                $(this).addClass("selected");
//                languageType = 3;
//            }
//        });
//
//    } else if (userLang == language[0]) {
//        $.each($(".menu_language ul li"), function() {
//            if ($(this).text() == "HE") {
//                $(this).addClass("selected");
//                languageType = 2;
//            }
//        });
//    } else {
//        $.each($(".menu_language ul li"), function() {
//            if ($(this).text() == "EN") {
//                $(this).addClass("selected");
//                languageType = 1;
//            }
//        });
//    }
//
//    $(".menu_language ul li").bind("click", function() {
//       changeLanguage($(this));
//    });

    //button reset 
    $("#main_reset").on("click", function() {
        showResetPopup();
    });

    $('.popup_reset_close').on("click", function() {
        hideResetPopup();
    });

    $('#button_reset_username').on("click", function() {
        var email = $("#reset_username").val();
        if (isValidEmailAddress(email)) {
            hideResetPopup();
            reset(email);
        } else {
            console.log('not valid');
        }
    });

    //button sign up
    $("#main_sign_up").on("click", function() {
        showSignUpPopup();
    });

    $(".popup_sign_up_close").on('click', function() {
        hideSignUpPopup();
    });

    $('#button_sign_up_cancel').on('click', function() {
        hideSignUpPopup();
    });

    $('#button_sign_up_done').on('click', function() {
        signUpNewUser();
    });

    //close popup window result(after reset password, after sign up, after login)
    $('.popup_result_close').on('click', function() {
        hideResult();
    });

    $(".cl_block_login_add_options input").bind("click", function() {
        var email = $("#username").val();
        var pass = $("#password").val();
        if (isValidEmailAddress(email)) {
            if (pass.length > 5) {
                login(email, pass);
            }
        }
    });

    $('.cab_sign_up').on('click', function() {
        showSignUpPopup();
    });

    //********************** MENU **************************//
    getCurrentMenu(sub);

    //********************** POPUP WORK ********************//    
    $('.menu_main li').on('click', function() {
        if ($(this).text() !== 'Hotels') {
            showWorkPopup();
        }
    });

    $('.footer_info_menu li').on('click', function() {
        if ($(this).text() !== 'Hotels') {
            showWorkPopup();
        }
    });

    $('.left_block_row div').on('click', function() {
        if ($(this).children('a').children('p').text() !== 'Hotels') {
            showWorkPopup();
        }
    });

    $('.popup_work_close').on('click', function() {
        hideWorkPopup();
    });
});

function loginSuccess(email, password) {
    if (isValidEmailAddress(email) && password.length > 4) {
        $.ajax({
            type: 'POST',
            url: urlBackend + 'login',
            data: {
                email: email,
                password: password
            },
            success: function(msg) {
                var json = $.parseJSON(msg);

                $(json).each(function(i, v) {
                    if (v.status == "ok") {
                        $('#login_user_name').val("");
                        $('#login_user_pass').val("");
                        hideLoginPopup();
                        createUserCabinet(v.user);
                    } else {
                        $('.login_error').html('E-mail or password incorrect.');
                    }
                });
            }
        });
    } else {
        $('.login_error').html('E-mail or password incorrect.');
    }
}

function createUserCabinet(user) {
    console.log(user);
    $('.cab_container ul').remove();
    var container = $('.cab_container');
    var newUl = $('<ul />');
    (newUl).append($('<li />').addClass('cab_welcome').html('Welcome ' + user.first_name + ' ' + user.last_name));
    (newUl).append($('<li />').addClass('cab_links').html('Balance: ' + user.balance));
    (newUl).append($('<li />').addClass('cab_links').html('Orders'));
    if (user.permissions == 4) {
        (newUl).append($('<li />').addClass('cab_admin').html('Admin Panel'));
    }
    (newUl).append($('<li />').addClass('cab_logout').html('Logout'));
     
    (container).append(newUl);
}

function getCurrentMenu(menu) {
    var selMenu = $('.menu_main ul li a').find('active');
    selMenu.removeClass('active');

    $('.menu_main ul li a').each(function(i, v) {
        if (menu === $(v).text()) {
            $(this).addClass('active');
        }
    });
}

function paypal(xTitle, amount) {
    console.log('hello');
    var data = {'cmd': '_xclick', 'business': 'viktor@travis.co.il', 'charset': 'UTF-8', 'lc': 'RU', 'item_name': xTitle,
        'amount': amount, 'currency_code': 'USD',
        'button_subtype': 'services', 'no_note': 0,
        'bn': 'PP-BuyNowBF:btn_buynowCC_LG.gif:NonHostedGuest'};
    $.ajax({
        type: 'POST',
        url: 'https://www.paypal.com/cgi-bin/webscr',
        data: data,
        success: function(msg) {
            console.log(msg);
        }
    });
}

function showLoginPopup() {
    var winWidth = $(window).width();
    var winHeight = $(window).height();

    $('#blackout').css({'width': winWidth + 'px', 'height': winHeight + 'px'});
    $('#blackout').show();

    var boxWidth = 400;
    var disWidth = (winWidth - boxWidth) / 2
    var disHeight = 150;
    $('.popup_login').css({'width': boxWidth + 'px', 'left': disWidth + 'px', 'top': disHeight + 'px'});
    $('.popup_login').show();
}

function hideLoginPopup() {
    $('#blackout').css({'width': 0 + 'px', 'height': 0 + 'px'});
    $('#blackout').hide();

    $('.popup_login').hide();
}

function showSignUpPopup() {
    var winWidth = $(window).width();
    var winHeight = $(window).height();

    $('#blackout').css({'width': winWidth + 'px', 'height': winHeight + 'px'});
    $('#blackout').show();

    var boxWidth = 400;
    var disWidth = (winWidth - boxWidth) / 2
    var disHeight = 150;
    $('.popup_sign_up').css({'width': boxWidth + 'px', 'left': disWidth + 'px', 'top': disHeight + 'px'});
    $('.popup_sign_up').show();
}

function hideSignUpPopup() {
    $('#blackout').css({'width': 0 + 'px', 'height': 0 + 'px'});
    $('#blackout').hide();

    $('.popup_sign_up').hide();
}

function showResetPopup() {
    var winWidth = $(window).width();
    var winHeight = $(window).height();

    $('#blackout').css({'width': winWidth + 'px', 'height': winHeight + 'px'});
    $('#blackout').show();

    var boxWidth = 400;
    var disWidth = (winWidth - boxWidth) / 2;
    var disHeight = 150;
    $('.popup_reset').css({'width': boxWidth + 'px', 'left': disWidth + 'px', 'top': disHeight + 'px'});
    $('.popup_reset').show();
}

function hideResetPopup() {
    $('#blackout').css({'width': 0 + 'px', 'height': 0 + 'px'});
    $('#blackout').hide();

    $('.popup_reset').hide();
}

function showWorkPopup(title, image) {
    var winWidth = $(window).width();
    var winHeight = $(window).height();

    $('#blackout').css({'width': winWidth + 'px', 'height': winHeight + 'px'});
    $('#blackout').show();

    var boxWidth = 400;
    var disWidth = (winWidth - boxWidth) / 2
    var disHeight = 150;
    $('.popup_work').css({'width': boxWidth + 'px', 'left': disWidth + 'px', 'top': disHeight + 'px'});
    $('.popup_work').show();

    $('.popup_reset_title span').empty();
    if (title) {
        $('.popup_reset_title span').text(title);
    } else {
        $('.popup_reset_title span').text('Я работаю над этим!');
    }

    $('.popup_image').empty();
    if (image) {
        $('.popup_image').append(image);
    } else {
        var rand = Math.floor((Math.random() * 5));

        $('.popup_image').append(work_images[rand]);
    }
}

function hideWorkPopup() {
    $('#blackout').css({'width': 0 + 'px', 'height': 0 + 'px'});
    $('#blackout').hide();

    $('.popup_work').hide();

    if (snd) {
        snd.pause();
    }
}

function isValidEmailAddress(emailAddress) {
    var pattern = new RegExp(/^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/i);
    return pattern.test(emailAddress);
}

//function changeLanguage(obj) {
//    var selected = $(".menu_language ul").find("li.selected");
//    (selected).removeClass("selected");
//    (obj).addClass("selected");
//
//    switch ($(obj).text()) {
//        case 'EN':
//            languageType = 1;
//            break;
//        case 'HE':
//            languageType = 2;
//            break;
//        case 'RU':
//            languageType = 3;
//            break;
//        default:
//            languageType = 1;
//            break;
//    }
//}